<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        position: absolute;
        left: 0;
        top: 0;
        width: 100px;
        height: 100px;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div></div>
    <script>
      var div = document.querySelector("div");
      var startX;
      var startY;
      var x;
      var y;
      // 1.给盒子注册 touchstart事件
      div.addEventListener("touchstart", function (e) {
        // 得到手指离页面的位置
        startX = e.targetTouches[0].pageX;
        startY = e.targetTouches[0].pageY;
        // 盒子的位置
        x = this.offsetLeft;
        y = this.offsetTop;
      });

      // 2.  手指移动
      div.addEventListener("touchmove", function (e) {
        var moveX = e.targetTouches[0].pageX - startX;
        var moveY = e.targetTouches[0].pageY - startY;
        this.style.left = moveX + x + "px";
        this.style.top = moveY + y + "px";
      });
    </script>
  </body>
</html>
